/*
 * @Description: 
 * @FilePath: /STM32/USER/bsp/dma.c
 * @Author: Wei Zhou
 * @Github: https://github.com/zromyk
 * @Date: 2021-04-28 15:25:19
 * @LastEditors: Wei Zhou
 * @LastEditTime: 2021-04-28 15:25:19
 * @Copyright: Copyright  2017 muyiro. All rights reserved.
 */
 
#include "dma.h"
#include "nvic.h"
#include "rcc.h"

void dma_tx_init(DMA_Dev *dma, uint32_t DR_addr, uint8_t *tx_buf, uint32_t tx_buf_size)
{
    DMA_InitTypeDef DMA_InitStruct;
    
    rcc_periph_clock_enable(dma->DMAx, dma->RCC_Periph_DMA);
        
    DMA_DeInit(dma->tx_stream);
    DMA_InitStruct.DMA_Channel               = dma->tx_channel;  
    
    DMA_InitStruct.DMA_DIR                   = DMA_DIR_MemoryToPeripheral;
    DMA_InitStruct.DMA_Mode                  = DMA_Mode_Normal;                 /* mode */
    DMA_InitStruct.DMA_PeripheralBaseAddr    = DR_addr;                         /* peripheral_address, address of DR, not value of DR */
    DMA_InitStruct.DMA_PeripheralInc         = DMA_PeripheralInc_Disable;
    DMA_InitStruct.DMA_PeripheralDataSize    = DMA_PeripheralDataSize_Byte;
    DMA_InitStruct.DMA_Memory0BaseAddr       = (uint32_t)tx_buf;                /* memory_address */
    DMA_InitStruct.DMA_Buffer_size           = tx_buf_size;                     /* memory_size */
    DMA_InitStruct.DMA_MemoryInc             = DMA_MemoryInc_Enable; 
    DMA_InitStruct.DMA_MemoryDataSize        = DMA_MemoryDataSize_Byte;  
    DMA_InitStruct.DMA_Priority              = DMA_Priority_Medium;
    
    DMA_InitStruct.DMA_FIFOMode              = DMA_FIFOMode_Disable;
    DMA_InitStruct.DMA_FIFOThreshold         = DMA_FIFOThreshold_Full;
    DMA_InitStruct.DMA_MemoryBurst           = DMA_MemoryBurst_Single;
    DMA_InitStruct.DMA_PeripheralBurst       = DMA_PeripheralBurst_Single;
    
    DMA_Init(dma->tx_stream, &DMA_InitStruct);
}

void dma_rx_init(DMA_Dev *dma, uint32_t DR_addr, uint8_t *rx_buf, uint32_t rx_buf_size)
{
    DMA_InitTypeDef DMA_InitStruct;
    
    rcc_periph_clock_enable(dma->DMAx, dma->RCC_Periph_DMA);
    
    DMA_DeInit(dma->rx_stream);
    DMA_InitStruct.DMA_Channel               = dma->rx_channel;  
    
    DMA_InitStruct.DMA_DIR                   = DMA_DIR_PeripheralToMemory;
    DMA_InitStruct.DMA_Mode                  = DMA_Mode_Normal;                 /* mode */
    DMA_InitStruct.DMA_PeripheralBaseAddr    = DR_addr;                         /* peripheral_address, address of DR, not value of DR */
    DMA_InitStruct.DMA_PeripheralInc         = DMA_PeripheralInc_Disable;
    DMA_InitStruct.DMA_PeripheralDataSize    = DMA_PeripheralDataSize_Byte;
    DMA_InitStruct.DMA_Memory0BaseAddr       = (uint32_t)rx_buf;                /* memory_address */
    DMA_InitStruct.DMA_Buffer_size           = rx_buf_size;                     /* memory_size */
    DMA_InitStruct.DMA_MemoryInc             = DMA_MemoryInc_Enable; 
    DMA_InitStruct.DMA_MemoryDataSize        = DMA_MemoryDataSize_Byte;  
    DMA_InitStruct.DMA_Priority              = DMA_Priority_VeryHigh;
    
    DMA_InitStruct.DMA_FIFOMode              = DMA_FIFOMode_Disable;
    DMA_InitStruct.DMA_FIFOThreshold         = DMA_FIFOThreshold_Full;
    DMA_InitStruct.DMA_MemoryBurst           = DMA_MemoryBurst_Single;
    DMA_InitStruct.DMA_PeripheralBurst       = DMA_PeripheralBurst_Single;
    
    DMA_Init(dma->rx_stream, &DMA_InitStruct);
}
